package com.news.web.servlet.admin;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.service.NewsService;
import com.news.service.impl.NewsServiceImpl;

/**
 * Servlet implementation class AdminNewsQueryServlet
 */
@WebServlet("/AdminNewsQueryToUpdateServlet")
public class AdminNewsQueryToUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String newsIdStr = request.getParameter("newsId");
		 // 进行数据格式转换
	    int newsId = 0;
	    try {
	        if (newsIdStr != null && !newsIdStr.isEmpty()) {
	            newsId = Integer.parseInt(newsIdStr);
	        }
	        
	    } catch (NumberFormatException e) {
	        // 处理数值转换异常的情况
	        e.printStackTrace(); // 记录日志或者向用户显示错误信息
	        request.setAttribute("newsQueryLikeList", null); // 或者设定为null
	        request.getRequestDispatcher("/admin/adminNewsInsertAndUpdate.jsp").forward(request, response);
	        return; // 结束方法，避免继续执行下面的逻辑
	    }
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 使用模糊查询
		News newsQuery = newsService.queryNews(newsId);
		System.out.println(newsQuery.toString());
		// 将数据存入
		request.setAttribute("newsQuery", newsQuery);
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminNewsInsertAndUpdate.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
